package com.runa.monitor.platinterface.backwork.impl;

import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.rccprotocol.dto.AffirmDeny;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

//import com.runa.monitor.platinterface.backwork.function.WriteBack;

/**
 * @author zhuj
 */
@Slf4j
public class RccTaskFunction {


    /**
     * 空任务
     */
    public static class EmptyTask implements TaskFunction {

        private int priority = 0;

        public EmptyTask() {
        }

        public EmptyTask(int priority) {
            this.priority = priority;
        }



        @Override
        public void setStatus(Object o) {

        }

        @Override
        public void doApply(CommStatus status, Object o) {

        }

        @Override
        public Object waitResult() {
            return null;
        }

        @Override
        public String getSerial() {
            return null;
        }

        @Override
        public int getPriority() {
            return priority;
        }
    }

    /**
     * 主动上报数据任务
     */

    public static class SyncFunction implements TaskFunction<Object, Object, Boolean> {

        private volatile Boolean result = null;

        private TaskFunction function;

        private Integer timeout = 5;

        private int priority;

        public SyncFunction(int priority) {
            this.priority = priority;
        }

        public SyncFunction(TaskFunction function, int priority) {
            this.function = function;
            this.priority = priority;

        }

        public SyncFunction(TaskFunction function, int timeout, int priority) {
            this.function = function;
            this.timeout = timeout;
            this.priority = priority;
        }

        @Override
        public void setStatus(Object step) {
            if (function != null) {
                function.setStatus(step);
            }
        }

        @Override
        public void doApply(CommStatus status, Object o) {
            if (o instanceof AffirmDeny) {
                result = ((AffirmDeny) o).getData();
            } else {
                result = status.equals(CommStatus.OK);
            }
            log.info("{} sync function result: {}", this, result);
            if (function != null) {
                function.doApply(status, o);
            }
        }

        @Override
        public Boolean waitResult() {
            while (result == null) {
                try {
                    log.info("{} result: {}", this, result);
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            return result != null && result;
        }

        @Override
        public String getSerial() {
            return null;
        }

        @Override
        public int getPriority() {
            return priority;
        }
    }

}
